'use strict';

const Service = require('egg').Service;

class Follow extends Service {
    async save(data, user){
        const { ctx } = this;

        data.follow_date = data.follow_date ? data.follow_date : ctx.helper.formatTime("YYYY-MM-DD");
        data.user_id = user.id;

        if(user.role != 10){
            data.user_role = user.role;
        }

        let rs = null;
        if(data.id){
            rs = await ctx.model.Follow.update(data, {where:{id: data.id}});
        } else {
            rs = await ctx.model.Follow.create(data);
        }

        // let list = await ctx.model.Follow.findAll();
        // console.log(list);


        //统计跟进次数
        let followNum = await ctx.model.FollowNum.findOrCreate({
            where: {
                follow_date: data.follow_date,
                user_role: parseInt(data.user_role),
                project_id: data.project_id
            },
            defaults: {
                follow_date: data.follow_date,
                user_role: parseInt(data.user_role),
                follow_num: 0,
                project_id: data.project_id
            }
        })
        let followNumItem = followNum[0];
        followNumItem.follow_num++;
        
        if(data.level == '1'){
            followNumItem.is_change = 1;
        }

        if(data.level == '10'){
            followNumItem.is_delay = 1;
        }

        await followNumItem.save();
        return rs;
    }

    /**
     * 跟进列表
     * @param {} params 
     * @returns 
     */
    async getList(params){
        const { ctx } = this;

        let where = {};
        if(params.project_id){
            where.project_id = params.project_id;
        }

        if(params.follow_date){
            where.follow_date = params.follow_date;
        }

        if(params.user_role){
            where.user_role = params.user_role;
        }

        if(params.user_id){
            where.user_id = params.user_id;
        }

        let pageSize = parseInt(params.page_size) || 10;
        let page = parseInt(params.page) || 1;
        let offset = (page - 1) * pageSize;

        let {count, rows} = await ctx.model.Follow.findAndCountAll({
            where: where, 
            limit: pageSize,
            offset: offset,
            order: [
                ["follow_date", 'desc'],
                ["create_time", 'desc'],
            ]
        })

        return {
            list: rows,
            count: count
        };
    }

    //获取跟进数量信息
    async getFollowNumInfo(params){
        const { ctx } = this;
        let start = params.start;
        let end = params.end;

        let Op = this.app.Sequelize.Op;

        let projectList = await ctx.service.project.getDoingList();


        let followNumList = await ctx.model.FollowNum.findAll({
            where: {
                follow_date : {
                    [Op.between]: [new Date(start), new Date(end)]
                }
                
            }
        })

        
        let roleList = [
            {role: 30, roleName: "策划"}
            ,{role: 100, roleName: "美术"}
            ,{role: 20, roleName: "程序"}
            ,{role: 40, roleName: "运营"}
        ];

        let infoList = new Array();
        //项目
        for(let x in projectList){
            let project = projectList[x];
            //角色
            for(let y in roleList){
                let role = roleList[y];

                let userName = await ctx.service.user.getUserName(project.head_user_id);

                let infoItem = {
                    projectId: project.id,
                    projectName: project.name,
                    principalUserId: project.head_user_id,
                    principalUserName: userName,
                    role: role.role,
                    roleName: role.roleName,
                };

                let startTime = (new Date(start)).getTime();
                let endTime = (new Date(end)).getTime();
                while(startTime <= endTime){
                    let dateStr = ctx.helper.msFormatTime("YYYY-MM-DD", startTime);
                    //infoItem[dateStr] = 0;

                    //根据日期和角色遍历跟进数据列表
                    for(let i in followNumList){
                        let numInfo = followNumList[i];

                        if(numInfo.project_id == project.id 
                            && numInfo.user_role == role.role 
                            && numInfo.follow_date == dateStr){
                                
                                infoItem[dateStr] = numInfo.follow_num;
                                break;
                        } 
                    }

                    startTime += 3600 * 24 * 1000;
                }

                infoList.push(infoItem);
            }
        }

        return infoList;
    }


    async getFollowNumList(params){
        const { ctx } = this;
        let start = params.start;
        let end = params.end;

        let Op = this.app.Sequelize.Op;

        let followNumList = await ctx.model.FollowNum.findAll({
            where: {
                follow_date : {
                    [Op.between]: [new Date(start), new Date(end)]
                }
                
            }
        })

        return followNumList;
    }


}

module.exports = Follow;